<?php
/*
 * 主要功能:
 *
 * 56.com - PHP - class.wc.php
 * coding:夏一平
 * 创建时间:2006-11-28
 */
class wc
{
	static protected $fileDir 	= C_HTML;
	static protected $pageName	= "show_flv";
	static protected $minSize	= 10240;//最少字节数
	static protected $dirCount	= 30;
	static protected $cacheTime	= 300;//秒
	static protected $outEnd	= ".html";
	static protected $endDoing	=  false;
	///////
	static protected $_fileName;
	static protected $_stop			= false;
	static protected $_tmpFileTime	= 30;//touch最长时间 一般30-60秒
	//////
	static public function cache($valueArray=array(),$config=array(),$clean = false)
	{
		$time 		= time();
		$makeHtml	= false;
		self::set($config);		
		
		$fileName 	=self::getFileName($valueArray);	
		if($clean == 'clean')
		{
			@unlink($fileName);
			exit();
			return;
		}elseif($clean == 'c')
		{
			@unlink($fileName);
			$fileMTime 	=0;
			$fileMSize 	=0;
		}elseif($clean)
		{
			return;
		}else
		{	
			$fileMTime 	=@filemtime(ROOT_DIR.$fileName);
			$fileMSize 	=@filesize(ROOT_DIR.$fileName);
		}
		header("fileMTime: ".$fileMTime);
		header("fileMSize: ".$fileMSize);
		if (!file_exists(ROOT_DIR.$fileName) 
			or ($fileMTime<$time-self::$cacheTime)
			or ($fileMSize==0 	and $fileMTime<$time-self::$_tmpFileTime)
			or ($fileMSize>0 	and $fileMSize<self::$minSize))
		{ 
			//$fd=fopen($fileName,'w');
			//fclose($fd);
			touch(ROOT_DIR.$fileName);
			$makeHtml = true;
		}
		if($makeHtml)
		{
			ob_start('wc__callback');
			//ob_start(array(&wc, 'callback'));
			//ob_start(array(&$this, 'webCache_step2'));
			ob_implicit_flush(1);
		}else if(self::$endDoing)
		{
			if(self::$endDoing == 'in')
			{
				self::in();
			}else if(self::$endDoing == 'go')
			{
				self::go();
			}else if(self::$endDoing == 'go?')
			{
				self::go(1);
			}
		}else
		{
			self::in();
		}
	}
	/*
	 * 功能:设定 
	 */
	static public function set(&$config)
	{
		if(is_array($config))
		{
			foreach($config as $k => $v)
			{
				if(isset(self::$$k))self::$$k = $v;
			}	
		}
	}
	static public function setEndDoing($endDoing)
	{
		self::$endDoing	= $endDoing;
	}
	static public function stop()
	{
		self::$_stop	= true;
	}
	/*
	 * 功能:写文件
	 */
	protected function write($fileName,&$content)
	{
		header("write: in");
		if(strlen($content)>self::$minSize)
		{
			header("write: do");
			$fd=fopen($fileName,'w');
			if($fd)
			{
				fwrite($fd,$content);
				fclose($fd);
				return;
			}
		}
		//否则
		@unlink($fileName);
	}
	
	protected function in()
	{
		if(self::$_stop)
		{
			header("Expires: ".gmdate("D, d M Y H:i:s", time())." GMT");
			header("Cache-Control: max-age=0");
			header("wc_stop: true");
		}else
		{
			header("Expires: ".gmdate("D, d M Y H:i:s", time()+self::$cacheTime)." GMT");
			header("Cache-Control: max-age=".self::$cacheTime);
		}
		if($_GET['e'])header("xypin: ".ROOT_DIR.self::$_fileName);
		readfile(ROOT_DIR.self::$_fileName);
		exit();
	}
	protected function go($a = 0)
	{
		$tmp =self::$_fileName;
		if($a)
		{
			$tmp =$tmp.'?'.time();
		}
		header("Location: ".$tmp);
		exit();
	}
	/*
	 * 功能:参数设定与排序
	 */
	protected function getFileName(&$valueArray)
	{
		if(is_array($valueArray) && count($valueArray))
		{
			//文件名
			$fileName    = 'c';
			//目录
			$fileNameDir = self::$fileDir.self::$pageName.'/';
			g::dir(ROOT_DIR.$fileNameDir);
			//数组排序
			arsort($valueArray);		
			foreach($valueArray as $k=>&$v)
			{
				if($v)
				{
					$v 		= explode('@',$v);
					$v 		= (count($v)>1) ? $v[1] : $v[0];
					$fileName .= '&'.$k.'='.$v;
					$len      = strlen($v);
					$len      = $len>5?5:$len;
					for ($i = 0; $i<$len; $i++)
					{
						$VarOrd1 += ord($v[$i])*$i;  
						$VarOrd2 += ord($v[$i])*($i*2+1);			
					}
				}
			}
			$VarOrd1 %= self::$dirCount; 
			if(self::$dirCount > 10)
			{
				$VarOrd2 	 %= self::$dirCount;
				$fileNameDir .= $VarOrd1.'/'.$VarOrd2.'/';
			}else
			{
				$fileNameDir .=$VarOrd1.'/';
			}
			g::dir(ROOT_DIR.$fileNameDir,true);
			self::$_fileName = $fileNameDir.$fileName.self::$outEnd;
			return self::$_fileName;
		}else
		{
			self::$_fileName =self::$fileDir.self::$pageName.self::$outEnd;
			return self::$_fileName;
		}
	}
	static public function callback(&$buffer)
	{
		header("callback: 1");
		if(self::$_stop || strlen($buffer)<self::$minSize)
		{
			header("Expires: ".gmdate("D, d M Y H:i:s", time())." GMT");
			header("Cache-Control: max-age=0");
			header("wc_stop: true");
		}else
		{
			header("Expires: ".gmdate("D, d M Y H:i:s", time()+self::$cacheTime)." GMT");
			header("Cache-Control: max-age=".self::$cacheTime);
			self::write(ROOT_DIR.self::$_fileName,$buffer);
		}
		if($_GET['e']) header("xypm: ".ROOT_DIR.self::$_fileName);
		return $buffer;
	}
}
function wc__callback($buffer)
{
	header("wc__callback: 23");
	return wc::callback($buffer);
}
?>